<style lang="less">
    @import 'login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="username">
                            <Input v-model="form.username" placeholder="请输入用户名">
                            <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input v-model="form.password" type="password" placeholder="请输入密码">
                            <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                            <!--<Button @click="handleSubmit" type="primary" long>注册</Button>-->
                        </FormItem>
                    </Form>
                    <!--<p class="login-tip">输入任意用户名和密码即可</p>-->
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
    import Cookies from 'js-cookie';
    import {login} from '../../api'

    export default {
        data() {
            return {
                form: {
                    username: 'soysauce',
                    password: ''
                },
                rules: {
                    username: [
                        {required: true, message: '账号不能为空', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            handleSubmit() {
                var _this=this;
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        login(_this.form)
                            .then(res => {
                                sessionStorage.setItem('access_token', res.access_token);
                                Cookies.set('user', this.form.username);
                                Cookies.set('password', this.form.password);
                                Cookies.set('access_token', res.access_token);
                                //todo 这里可以做权限处理保存一个权限 比如这里存储权限为1 则路由里面的权限为1的会被显示 路由里面位了隐藏菜单，给隐藏的菜单设置 acess=-1
//                                Cookies.set('access',1)

                                this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
                                this.$router.push({
                                    name: 'home_index'
                                })
                            })
//                        Cookies.set('user', this.form.username);
//                        Cookies.set('password', this.form.password);
//                        this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
//                        if (this.form.username === 'iview_admin') {
//                            Cookies.set('access', 0);
//                        } else {
//                            Cookies.set('access', 1);
//                        }
//                        this.$router.push({
//                            name: 'home_index'
//                        });
                    }
                });
            }
        }
    };
</script>

<style>

</style>
